home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pyshared / PIL / ImageChops.py < prev    next >
Text File  |  2006-12-03  |  7KB  |  303 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: ImageChops.py 2134 2004-10-06 08:55:20Z fredrik $
  4. #
  5. # standard channel operations
  6. #
  7. # History:
  8. # 1996-03-24 fl   Created
  9. # 1996-08-13 fl   Added logical operations (for "1" images)
  10. # 2000-10-12 fl   Added offset method (from Image.py)
  11. #
  12. # Copyright (c) 1997-2000 by Secret Labs AB
  13. # Copyright (c) 1996-2000 by Fredrik Lundh
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17.  
  18. import Image
  19.  
  20. ##
  21. # The <b>ImageChops</b> module contains a number of arithmetical image
  22. # operations, called <i>channel operations</i> ("chops"). These can be
  23. # used for various purposes, including special effects, image
  24. # compositions, algorithmic painting, and more.
  25. # <p>
  26. # At this time, channel operations are only implemented for 8-bit
  27. # images (e.g. "L" and "RGB").
  28. # <p>
  29. # Most channel operations take one or two image arguments and returns
  30. # a new image.  Unless otherwise noted, the result of a channel
  31. # operation is always clipped to the range 0 to MAX (which is 255 for
  32. # all modes supported by the operations in this module).
  33. ##
  34.  
  35. ##
  36. # Return an image with the same size as the given image, but filled
  37. # with the given pixel value.
  38. #
  39. # @param image Reference image.
  40. # @param value Pixel value.
  41. # @return An image object.
  42.  
  43. def constant(image, value):
  44.     "Fill a channel with a given grey level"
  45.  
  46.     return Image.new("L", image.size, value)
  47.  
  48. ##
  49. # Copy image.
  50. #
  51. # @param image Source image.
  52. # @return A copy of the source image.
  53.  
  54. def duplicate(image):
  55.     "Create a copy of a channel"
  56.  
  57.     return image.copy()
  58.  
  59. ##
  60. # Inverts an image
  61. # (MAX - image).
  62. #
  63. # @param image Source image.
  64. # @return An image object.
  65.  
  66. def invert(image):
  67.     "Invert a channel"
  68.  
  69.     image.load()
  70.     return image._new(image.im.chop_invert())
  71.  
  72. ##
  73. # Compare images, and return lighter pixel value
  74. # (max(image1, image2)).
  75. # <p>
  76. # Compares the two images, pixel by pixel, and returns a new image
  77. # containing the lighter values.
  78. #
  79. # @param image1 First image.
  80. # @param image1 Second image.
  81. # @return An image object.
  82.  
  83. def lighter(image1, image2):
  84.     "Select the lighter pixels from each image"
  85.  
  86.     image1.load()
  87.     image2.load()
  88.     return image1._new(image1.im.chop_lighter(image2.im))
  89.  
  90. ##
  91. # Compare images, and return darker pixel value
  92. # (min(image1, image2)).
  93. # <p>
  94. # Compares the two images, pixel by pixel, and returns a new image
  95. # containing the darker values.
  96. #
  97. # @param image1 First image.
  98. # @param image1 Second image.
  99. # @return An image object.
  100.  
  101. def darker(image1, image2):
  102.     "Select the darker pixels from each image"
  103.  
  104.     image1.load()
  105.     image2.load()
  106.     return image1._new(image1.im.chop_darker(image2.im))
  107.  
  108. ##
  109. # Calculate absolute difference
  110. # (abs(image1 - image2)).
  111. # <p>
  112. # Returns the absolute value of the difference between the two images.
  113. #
  114. # @param image1 First image.
  115. # @param image1 Second image.
  116. # @return An image object.
  117.  
  118. def difference(image1, image2):
  119.     "Subtract one image from another"
  120.  
  121.     image1.load()
  122.     image2.load()
  123.     return image1._new(image1.im.chop_difference(image2.im))
  124.  
  125. ##
  126. # Superimpose positive images
  127. # (image1 * image2 / MAX).
  128. # <p>
  129. # Superimposes two images on top of each other. If you multiply an
  130. # image with a solid black image, the result is black. If you multiply
  131. # with a solid white image, the image is unaffected.
  132. #
  133. # @param image1 First image.
  134. # @param image1 Second image.
  135. # @return An image object.
  136.  
  137. def multiply(image1, image2):
  138.     "Superimpose two positive images"
  139.  
  140.     image1.load()
  141.     image2.load()
  142.     return image1._new(image1.im.chop_multiply(image2.im))
  143.  
  144. ##
  145. # Superimpose negative images
  146. # (MAX - ((MAX - image1) * (MAX - image2) / MAX)).
  147. # <p>
  148. # Superimposes two inverted images on top of each other.
  149. #
  150. # @param image1 First image.
  151. # @param image1 Second image.
  152. # @return An image object.
  153.  
  154. def screen(image1, image2):
  155.     "Superimpose two negative images"
  156.  
  157.     image1.load()
  158.     image2.load()
  159.     return image1._new(image1.im.chop_screen(image2.im))
  160.  
  161. ##
  162. # Add images
  163. # ((image1 + image2) / scale + offset).
  164. # <p>
  165. # Adds two images, dividing the result by scale and adding the
  166. # offset. If omitted, scale defaults to 1.0, and offset to 0.0.
  167. #
  168. # @param image1 First image.
  169. # @param image1 Second image.
  170. # @return An image object.
  171.  
  172. def add(image1, image2, scale=1.0, offset=0):
  173.     "Add two images"
  174.  
  175.     image1.load()
  176.     image2.load()
  177.     return image1._new(image1.im.chop_add(image2.im, scale, offset))
  178.  
  179. ##
  180. # Subtract images
  181. # ((image1 - image2) / scale + offset).
  182. # <p>
  183. # Subtracts two images, dividing the result by scale and adding the
  184. # offset. If omitted, scale defaults to 1.0, and offset to 0.0.
  185. #
  186. # @param image1 First image.
  187. # @param image1 Second image.
  188. # @return An image object.
  189.  
  190. def subtract(image1, image2, scale=1.0, offset=0):
  191.     "Subtract two images"
  192.  
  193.     image1.load()
  194.     image2.load()
  195.     return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
  196.  
  197. ##
  198. # Add images without clipping
  199. # ((image1 + image2) % MAX).
  200. # <p>
  201. # Adds two images, without clipping the result.
  202. #
  203. # @param image1 First image.
  204. # @param image1 Second image.
  205. # @return An image object.
  206.  
  207. def add_modulo(image1, image2):
  208.     "Add two images without clipping"
  209.  
  210.     image1.load()
  211.     image2.load()
  212.     return image1._new(image1.im.chop_add_modulo(image2.im))
  213.  
  214. ##
  215. # Subtract images without clipping
  216. # ((image1 - image2) % MAX).
  217. # <p>
  218. # Subtracts two images, without clipping the result.
  219. #
  220. # @param image1 First image.
  221. # @param image1 Second image.
  222. # @return An image object.
  223.  
  224. def subtract_modulo(image1, image2):
  225.     "Subtract two images without clipping"
  226.  
  227.     image1.load()
  228.     image2.load()
  229.     return image1._new(image1.im.chop_subtract_modulo(image2.im))
  230.  
  231. ##
  232. # Logical AND
  233. # (image1 and image2).
  234.  
  235. def logical_and(image1, image2):
  236.     "Logical and between two images"
  237.  
  238.     image1.load()
  239.     image2.load()
  240.     return image1._new(image1.im.chop_and(image2.im))
  241.  
  242. ##
  243. # Logical OR
  244. # (image1 or image2).
  245.  
  246. def logical_or(image1, image2):
  247.     "Logical or between two images"
  248.  
  249.     image1.load()
  250.     image2.load()
  251.     return image1._new(image1.im.chop_or(image2.im))
  252.  
  253. ##
  254. # Logical XOR
  255. # (image1 xor image2).
  256.  
  257. def logical_xor(image1, image2):
  258.     "Logical xor between two images"
  259.  
  260.     image1.load()
  261.     image2.load()
  262.     return image1._new(image1.im.chop_xor(image2.im))
  263.  
  264. ##
  265. # Blend images using constant transparency weight.
  266. # <p>
  267. # Same as the <b>blend</b> function in the <b>Image</b> module.
  268.  
  269. def blend(image1, image2, alpha):
  270.     "Blend two images using a constant transparency weight"
  271.  
  272.     return Image.blend(image1, image2, alpha)
  273.  
  274. ##
  275. # Create composite using transparency mask.
  276. # <p>
  277. # Same as the <b>composite</b> function in the <b>Image</b> module.
  278.  
  279. def composite(image1, image2, mask):
  280.     "Create composite image by blending images using a transparency mask"
  281.  
  282.     return Image.composite(image1, image2, mask)
  283.  
  284. ##
  285. # Offset image data.
  286. # <p>
  287. # Returns a copy of the image where data has been offset by the given
  288. # distances.  Data wraps around the edges.  If yoffset is omitted, it
  289. # is assumed to be equal to xoffset.
  290. #
  291. # @param image Source image.
  292. # @param xoffset The horizontal distance.
  293. # @param yoffset The vertical distance.  If omitted, both
  294. #    distances are set to the same value.
  295. # @return An Image object.
  296.  
  297. def offset(image, xoffset, yoffset=None):
  298.     "Offset image in horizontal and/or vertical direction"
  299.     if yoffset is None:
  300.         yoffset = xoffset
  301.     image.load()
  302.     return image._new(image.im.offset(xoffset, yoffset))
  303.